'''
Created on Oct 23, 2011

@author: alonyado
'''
from threading import Thread
from http_mjpeg_client import CommunicationHandler
import traceback

def copyDictionary(d):
    d_new = dict()
    for k,v in d.iteritems():
        d_new[k] = v
    return d_new

class CommunicationThread(Thread):
    
    def __init__(self, threadName, config):
        Thread.__init__(self)
        self.setDaemon(1)
        self.name = threadName
        self.comm = CommunicationHandler(config)
        self.running = True
        cconfig = copyDictionary(config)
        self.config = cconfig
        
    def stop(self):
        self.running = False
        
    def run(self):
        print 'Communication Thread "%s" Is Active'%self.name
	while (self.running):
  	    if not(self.comm.isConnected):
  	        try:
  	            self.comm.connect()
  	            self.comm.getData()
  	        except:
  	            traceback.print_exc()
  	    else:
  	        try:
  	            self.comm.getData()
  	        except:
	            traceback.print_exc()
  	try:
  	    self.comm.close()
  	except:
  	    traceback.print_exc()
        print 'Communication Thread "%s" Has Stopped!'%self.name
            
            
        
